# -*- coding: cp950 -*-
import wx
from Constant import *
from PublicMethod import *
'''
The door animation - Split. when calling closing door method, the image will move from down to top 

gradually and one by one from left to right. And each image refers to one word in total message. 

When the door closes, user can call method of opening door momentarily. As opening, the image will

move to left until the image disappears one by one from left to right. As soon as the final image

finishes moving, this object will be fully destroyed.
'''
class DoorAnimation_Split(wx.Panel):
    '''
    Constructor : DoorAnimation_Split
        panel : add this object to panel
        mainMap : object pointer - MainMap
        doorType : type of door animation
    '''
    def __init__(self, panel, mainMap, doorType):
        wx.Panel.__init__(self, panel, -1)
        self.Show(False)
        self._001_initialInstanceVariable(panel, mainMap, doorType)
        self._002_bindAction()
    '''
    function _001 : initialize the variables used for this object
        panel : add this object to panel
        mainMap : object pointer - MainMap
        doorType : type of door animation
    '''
    def _001_initialInstanceVariable(self, panel, mainMap, doorType):
        #the panel
        self.panel = panel
        #object pointer - MainMap
        self.mainMap = mainMap
        #door type
        self.doorType = doorType
        #split image
        self.listSplitImg = []
        #timer of close door
        self.timer_closeDoor = wx.Timer()
        #timer of open door
        self.timer_openDoor = wx.Timer()
        #list of image file name
        self.listImgName = [DIR_IMG_DOOR + "/door_youDie_6seg.png",
                            DIR_IMG_DOOR + "/door_youWin_6seg.png",
                            DIR_IMG_DOOR + "/door_start_6seg.png",
                            DIR_IMG_DOOR + "/door_finish_6seg.png"]
        #index of image that is is moving now
        self.curMoveimgIndex = 0
        #split number of full image
        self.splitNum = 6
    '''
    function _002 : bind object event to specific method
    '''
    def _002_bindAction(self):
        #bind timer of closing door event to method
        self.timer_closeDoor.Bind(wx.EVT_TIMER, self.SLOT002_closeDoor)
        #bind timer of opening door event to method
        self.timer_openDoor.Bind(wx.EVT_TIMER, self.SLOT001_openDoor)
        #bind event of starting to open door to method
        self.Bind(EVT_OPEN_DOOR, self.SLOT003_startOpenDoor)
    '''
    function _003 : create each image chip of door
    '''
    def _003_createSplitImg(self, isCurOpen):
        self._008_clearListSplitImg()
        #get original image
        str_fileName = self.listImgName[self.doorType]
        imgOrg  = wx.Image(str_fileName, PublicMethod.ACT004_getImageWXType(str_fileName))
        #split the image into each slice
        sliceSize = (imgOrg.GetSize()[0] / self.splitNum, imgOrg.GetSize()[1])
        index = 0
        while index < self.splitNum :
            imgSlice = imgOrg.GetSubImage(wx.Rect(sliceSize[0] * index, 0, sliceSize[0], sliceSize[1]))
            imgSlice = imgSlice.ConvertToBitmap()
            if isCurOpen :
                pos = (index * sliceSize[0], 0)
            else :
                pos = (index * sliceSize[0], self.panel.GetSize()[1])
            bmp = wx.StaticBitmap(parent = self.panel, bitmap = imgSlice)
            bmp.SetPosition(pos)
            bmp.Show(False)
            bmp.Show(True)
            self.listSplitImg.append(bmp)
            index = index + 1
        #destroy original image
        imgOrg.Destroy()
    '''
    function _004 : when finishing closing the door, this method will be called
    '''
    def _004_finishCloseDoor(self):
        self.mainMap.panelMap.Show(False)
        if self.doorType != DOOR_TYPE_FINISH :
            self.mainMap.ACT011_setFunctionalBtnAble(FUNC_CONTINUE, True)
        else :
            self.mainMap.ACT011_setFunctionalBtnAble(FUNC_CONTINUE, False)
        self.mainMap.ACT011_setFunctionalBtnAble(FUNC_BURST_MYSELF, False)
        self.mainMap.ACT011_setFunctionalBtnAble(FUNC_LEAVE, True)
        self.mainMap._003_clearMap()
    '''
    function _005 : when finishing opening the door, this method will be called
    '''
    def _005_finishOpenDoor(self):
        self.mainMap.ACT009_startPlaying()
        self._007_destroy()
    '''
    function _006 : move method
        vectorMove : the vector of direction (1,0) - move on x-axle ; (0,1) - move on y-axle
        endPos : the final position that current image moves to
        isAdd : decide the direction
    '''
    def _006_Move(self, vectorMove, endPos, isAdd):
        isAtTop = False
        #get position of current split image in index - curMoveimgIndex
        posX = self.listSplitImg[self.curMoveimgIndex].GetPosition()[0]
        posY = self.listSplitImg[self.curMoveimgIndex].GetPosition()[1]
        #set next position
        if isAdd : #increase position
            posX = posX + vectorMove[0]
            posY = posY + vectorMove[1]
            if posX >= endPos[0] and  posY >= endPos[1]:  #arrive end position
                posX = endPos[0]
                posY = endPos[1]
                isAtTop = True 
        else :  #decrease position
            posX = posX - vectorMove[0]
            posY = posY - vectorMove[1]
            if posX <= endPos[0] and  posY <= endPos[1]:  #arrive end position
                posX = endPos[0]
                posY = endPos[1]
                isAtTop = True 
        self.listSplitImg[self.curMoveimgIndex].SetPosition((posX, posY))
        return isAtTop
    '''
    function _007 : destroy all object of this class
    '''
    def _007_destroy(self):
        #destroy split image
        self._008_clearListSplitImg()
        #destroy timer
        self.timer_closeDoor.Stop()
        self.timer_closeDoor.Destroy()
        self.timer_openDoor.Stop()
        self.timer_openDoor.Destroy()
        self.Destroy()
        del self
    '''
    function _008 : clear all image object in list - listSplitImg
    '''
    def _008_clearListSplitImg(self):
        index = 0
        while index < self.listSplitImg.__len__() :
            self.listSplitImg[index].Destroy()
            index = index + 1
        self.listSplitImg = []
    '''
    function ACT001 : start to open the door
    '''
    def ACT001_startToOpenDoor(self):
        self._003_createSplitImg(True)
        self.curMoveimgIndex = 0
        self.timer_openDoor.Start(30)
    '''
    function ACT002 : start to close the door
    '''
    def ACT002_startToCloseDoor(self):
        self._003_createSplitImg(False)
        self.curMoveimgIndex = 0
        self.timer_closeDoor.Start(15)
    '''
    function GET001 : get instance variable - doorType
    '''
    def GET001_doorType(self):
        return self.doorType
    '''
    function SLOT001 : slot of opening the door gradually
    '''
    def SLOT001_openDoor(self, event):
        if self._006_Move((A_TIME_MOVE_DIST, 0), (0 - self.listSplitImg[self.curMoveimgIndex].GetSize()[0], 
                           self.listSplitImg[self.curMoveimgIndex].GetPosition()[1]), False) :
            if self.curMoveimgIndex < self.splitNum - 1 :  #set split image of next index
                self.curMoveimgIndex = self.curMoveimgIndex + 1
            else :  #door-closing stop
                self.timer_openDoor.Stop()
                self._005_finishOpenDoor()
    '''
    function SLOT002 : slot of closing the door gradually
    '''
    def SLOT002_closeDoor(self, event):
        if self._006_Move((0, A_TIME_MOVE_DIST), (self.listSplitImg[self.curMoveimgIndex].GetPosition()[0], 0), False) :
            if self.curMoveimgIndex < self.splitNum - 1 :  #set split image of next index
                self.curMoveimgIndex = self.curMoveimgIndex + 1
            else :  #door-closing stop
                self.timer_closeDoor.Stop()
                self._004_finishCloseDoor()
    '''
    function SLOT001 : slot of starting to open the door
    '''            
    def SLOT003_startOpenDoor(self, event):
        #set next step
        if self.doorType == DOOR_TYPE_LOST :
            self.mainMap.ACT008_startCurStep()
        elif self.doorType == DOOR_TYPE_WIN :
            self.mainMap.ACT012_startNextStep()
        self.mainMap._009_focusMapPositionToMaster()
        self.ACT001_startToOpenDoor()
        self.mainMap.panelMap.Show(True)